package cz.gelion.shylock.invoice.web.subj

import cz.gelion.shylock.invoice.entity.Contact
import org.apache.wicket.model.IModel
import org.apache.wicket.markup.html.panel.Panel
import cz.gelion.shylock.invoice.Log
import cz.gelion.shylock.invoice.web.comp.TaForm
import org.apache.wicket.model.CompoundPropertyModel
import org.apache.wicket.markup.html.form.DropDownChoice
import org.apache.wicket.model.LoadableDetachableModel
import cz.gelion.shylock.invoice.web.Application
import org.apache.wicket.markup.html.form.TextArea
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink
import org.apache.wicket.ajax.AjaxRequestTarget
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.form.IChoiceRenderer
import cz.gelion.shylock.invoice.entity.ContactType

class ContactEditorPanel(id:String, model:IModel[Contact]) extends Panel(id,model) with Log {

  val form = new TaForm("form", new CompoundPropertyModel[Contact](model))
  add(form)
  form.add(new DropDownChoice("ctype", new LoadableDetachableModel[java.util.List[ContactType]] {
	  override def load:java.util.List[ContactType] = Application.em.createQuery("select t from ContactType t", classOf[ContactType]).getResultList()
  }, new IChoiceRenderer[ContactType] {
    
    override def getDisplayValue(t:ContactType):Object = t.getId
    
    override def getIdValue(t:ContactType, i:Int):String =  t.getId + "-" + i
    
  }).setRequired(true))
  form.add(new TextArea("addr").setRequired(true))
  form.add(new AjaxSubmitLink("link.ok") {
	 override def onSubmit(target:AjaxRequestTarget, form:Form[_]) {
	   log.trace("AjaxSubmitLink[link.ok].onSubmit")
	   //TODO
	 } 
	  override def onError(target:AjaxRequestTarget, form:Form[_]) {
	   log.trace("AjaxSubmitLink[link.ok].onError")
	   //TODO
	 } 
  });
}